package com.unlcn.ils.kas.service.order.impl;

import com.alibaba.fastjson.JSONObject;
import com.unlcn.ils.kas.dao.model.order.OrderOtdLog;
import com.unlcn.ils.kas.dao.model.order.OrderOtdLogDetail;
import com.unlcn.ils.kas.service.order.HoldingOrderService;
import com.unlcn.ils.kas.service.order.PushOrderInfoService;
import com.unlcn.ils.kas.service.order.SharesOrderService;
import com.unlcn.ils.kas.service.util.ErpUrl;
import com.unlcn.ils.kas.service.util.HttpClientUtil;
import com.unlcn.ils.kas.service.util.enums.order.CustomNoEnum;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * Created by lenovo on 2018/5/9.
 */
@Service
public class PushOrderInfoServiceImpl implements PushOrderInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(PushOrderInfoServiceImpl.class);

    @Autowired
    private SharesOrderService sharesOrderService;

    @Autowired
    private HoldingOrderService holdingOrderService;

    @Autowired
    private ErpUrl erpUrl;

    @Override
    public List<Map<String, String>> pushOrderByOrderNo(String[] sysOrderNos) {

        List<Map<String, String>> mapList = new ArrayList<>();

        for (String sysOrderNo : sysOrderNos) {

            Map<String, String> result = new HashMap<>();

            try {
                // 获取订单详情
                List<NameValuePair> orderDetailParams = new ArrayList<>();
                orderDetailParams.add(new BasicNameValuePair("vcdn", sysOrderNo));
                String getOrderDetailResult = HttpClientUtil.get(erpUrl.getGetErpOrderDetail(), orderDetailParams, 600000000);
                if (StringUtils.isEmpty(getOrderDetailResult))
                    continue;

                JSONObject json = JSONObject.parseObject(getOrderDetailResult);
                Object obj = json.get("data");
                LOGGER.info("(" + sysOrderNo + ")" + "详情: " + obj);
                if (obj == null) {
                    result.put("sysOrderNo", sysOrderNo);
                    result.put("result", "不存在此订单号");
                    mapList.add(result);
                    continue;
                }
                Map map  = (Map)obj;
                if (map.get("icustomerid").toString().equals(CustomNoEnum.GF.getValue())) {
                    pushGfOrder(obj, sysOrderNo);
                } else if (map.get("icustomerid").toString().equals(CustomNoEnum.KG.getValue())) {
                    pushKgOrder(obj, sysOrderNo);
                }


                result.put("sysOrderNo", sysOrderNo);
                result.put("result", "成功拉取");
            } catch (Exception e) {
                LOGGER.error("拉取订单" + sysOrderNo + "发生异常", e);
                result.put("sysOrderNo", sysOrderNo);
                result.put("result", "拉取失败");
                result.put("msg", e.getMessage());
                continue;
            }

            mapList.add(result);
        }

        return mapList;
    }

    private void pushGfOrder(Object obj, String sysOrderNo) {
        OrderOtdLog orderOtdLog = sharesOrderService.analysisOrder((Map)obj);
        orderOtdLog.setCallUrl(erpUrl.getGetErpKgOrderNo());

        // 获取订单在途信息
        List<NameValuePair> onWayParams = new ArrayList<>();
        onWayParams.add(new BasicNameValuePair("vcdn", sysOrderNo));
        String onWayResult = HttpClientUtil.get(erpUrl.getGetErpOnway(), onWayParams, 600000000);

        List<OrderOtdLogDetail> orderOtdLogDetailList = new ArrayList<>();
        if (!StringUtils.isEmpty(onWayResult)) {
            // 将在途信息转换成map集合
            List<Map> onWayList = sharesOrderService.getMap(onWayResult);

            if (!CollectionUtils.isEmpty(onWayList)) {
                for (Map onWayMap : onWayList) {
                    OrderOtdLogDetail orderOtdLogDetail = sharesOrderService.analysisOnWayInfo(onWayMap);
                    orderOtdLogDetailList.add(orderOtdLogDetail);
                }
            }
        }

        // 保存订单详细信息、在途信息
        sharesOrderService.saveOrderInfo(orderOtdLog, orderOtdLogDetailList);
    }

    private void pushKgOrder(Object obj, String sysOrderNo) {

        OrderOtdLog orderOtdLog = holdingOrderService.analysisOrder((Map)obj);
        orderOtdLog.setCallUrl(erpUrl.getGetErpKgOrderNo());

        // 获取订单在途信息
        List<NameValuePair> onWayParams = new ArrayList<>();
        onWayParams.add(new BasicNameValuePair("vcdn", sysOrderNo));
        String onWayResult = HttpClientUtil.get(erpUrl.getGetErpOnway(), onWayParams, 600000000);

        List<OrderOtdLogDetail> orderOtdLogDetailList = new ArrayList<>();
        if (!StringUtils.isEmpty(onWayResult)) {
            // 将在途信息转换成map集合
            List<Map> onWayList = holdingOrderService.getMap(onWayResult);

            if (!CollectionUtils.isEmpty(onWayList)) {
                for (Map onWayMap : onWayList) {
                    OrderOtdLogDetail orderOtdLogDetail = holdingOrderService.analysisOnWayInfo(onWayMap);
                    orderOtdLogDetailList.add(orderOtdLogDetail);
                }
            }
        }

        // 保存订单详细信息、在途信息
        holdingOrderService.saveOrderInfo(orderOtdLog, orderOtdLogDetailList);
    }
}
